var $ = layui.$;
var table = layui.table;
var layer = layui.layer;
var form = layui.form;
/*表格头部工具栏事件*/
table.on("toolbar(datalist)",function (obj){
    var event = obj.event;
    if ("check"==event){
        /**查询
         * 获取输入框中的内容
         * 根据输入框的内容查询结果
         * 表格重新加载数据
         * */
        var text = $("[name='checktext']").val();
        table.reload("datalist",{
            where:{checktext:text}
        });
    }
    if ("add"==event){

        layer.open({
            type: 1,
            title: '新增用户',
            shadeClose: false,
            // shade: true,
            maxmin: true, //开启最大化最小化按钮
            area: ['500px', '300px'],
            btn:["保存","重置"],
            // content: './index.html'
            content:$("#windo"),
            btn1:function () {
                console.log(form.val("addDepartmentForm"))
                $.ajax({
                    url:"../department/add",
                    data:form.val("addDepartmentForm"),
                    success:function (res) {
                        if (res){
                            table.reload("datalist");
                            layer.closeAll();
                            layer.msg("保存成功")
                        }else {
                         layer.msg("保存失败，请重试");

                        }
                    }
                })
            }


        });
    }



});


//行内工具栏事件
table.on("tool(datalist)", function(obj){
    var data = obj.data;
    //console.log(obj)
    if(obj.event == "del"){
        layer.confirm('真的删除行么？', function(index){
            //确定后要执行的代码
            obj.del();
            layer.close(index);
            $.ajax({
                url:"../department/delete",
                data:{
                    id:data.id
                },
                success:function (res){
                    layer.msg("删除成功",{icon:1,time:3000});
                    table.reload("datalist");
                }
            })
            // layer.msg("删除成功");
        });
    } else if(obj.event === 'edit'){
        form.val("editDepartmentForm",data);
        layer.open({
            type: 1,
            title: '修改部门名',
            shadeClose: false,
            // shade: true,
            maxmin: true, //开启最大化最小化按钮
            area: ['500px', '300px'],
            btn:["提交","重置"],
            // content: './index.html'
            content:$("#windoedit"),
            btn1:function (){
                //获取表单数据
                var formd = form.val("editDepartmentForm");
                //发送到后台
                $.ajax({
                    url: "../department/edit",
                    data: formd,
                    success:function (res){
                        console.log(res)
                        if (res){
                            console.log(formd)
                            table.reload("datalist");
                            layer.msg("提交成功",{icon:1,time:3000});
                        }else {
                            layer.msg("保存失败，请重试");
                        }
                    }
                });
                layer.closeAll();
            }
        });
    }
});

